package com.infotel.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.StringTokenizer;

import com.infotel.media.Navigateur;
import com.infotel.media.UserNavigateur;

public class StatsLog {

    /**
     * @param args
     */
    public static void main(String[] args) {
        BufferedReader br = null;

        // byte[] buffer = new byte[2048];
        String line = null;
        String cheminSource = "C:\\a_workspace\\Multimedia\\files\\log.txt";

        String ip = "";
        String reste;
        String reste2 = "";
        String browser = "";
        String version = "";
        int compteurToken = 0;
        HashSet<UserNavigateur> listeUserNavigateur = new HashSet<UserNavigateur>();
        HashSet<Navigateur> listeNavigateur = new HashSet<Navigateur>();
        try {

            br = new BufferedReader(new FileReader(new File(cheminSource)));
            do {
                line = br.readLine();

                if (line != null) {

                    StringTokenizer st = new StringTokenizer(line, "-");
                    ip = st.nextToken();
                    reste = "";
                    while (st.hasMoreTokens()) {
                        reste += st.nextToken();
                    }

                    StringTokenizer st2 = new StringTokenizer(reste, "\"");
                    // System.out.println("nb:"+st2.countTokens());

                    compteurToken = 0;
                    while (st2.hasMoreTokens()) {

                        if (compteurToken == 5) {
                            reste2 = st2.nextToken();
                            StringTokenizer st3 = new StringTokenizer(reste2,
                                    " ");
                            reste2 = st3.nextToken();

                        } else {
                            st2.nextToken();
                        }
                        compteurToken++;

                    }
                    StringTokenizer st4 = new StringTokenizer(reste2, "/");
                    browser = st4.nextToken();
                    if (st4.hasMoreElements()) {
                        version = st4.nextToken();
                    } else {
                        version = "N/A";
                    }
                }

        
                UserNavigateur unav = new UserNavigateur(ip, browser, version);
                if (unav != null) {
                    listeUserNavigateur.add(unav);
                }

            } while (line != null);
            Navigateur nav = null;
            for (UserNavigateur n : listeUserNavigateur) {
                System.out.println(n.getIp() + ":" + n.getBrowser() + ":"
                        + n.getVersion());
          
                    nav = new Navigateur(n.getBrowser(), n.getVersion());
                    if (nav != null) {
                        listeNavigateur.add(nav);

                    }
                     
                
            }

for(Navigateur n :listeNavigateur){
    int occurrences = Collections.frequency(listeUserNavigateur, n);
    System.out.println(occurrences);
}
            System.out.println(listeUserNavigateur.size() + ":"
                    + listeNavigateur.size());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        /*
         * do { nb = is.read(); os.write(nb); } while (nb != -1);
         */

    }
  

}
